home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / DWYER.ZIP / POKER.TST / SETPOKER.C < prev   
C/C++ Source or Header  |  1996-02-20  |  5KB  |  147 lines

  1. /* ============ */
  2. /* setpoker.c    */
  3. /* ============ */
  4. #include <defcodes.h>
  5. #include <miscdefs.h>
  6. #include <pokrdefs.h>
  7. #include <math.h>
  8.  
  9. #define    ACT(X)    #X
  10.  
  11. #define    NEED_CARDS_PER_HAND(LO, HI) \
  12.     "Enter Number of Cards Per Hand ["ACT(LO)"-"ACT(HI)"]: "
  13. #define    NEED_NUMBER_UNIQUE_CARDS(LO, HI) \
  14.     "Enter Number of Unique Cards in Random Deck ["\
  15.         ACT(LO)"-"ACT(HI)"]: "
  16. #define    NEED_MIN_CELL_EXPECT(LO, HI) \
  17.     "Enter Minimum Cell Expectation ["ACT(LO)"-"ACT(HI)"]: "
  18.  
  19. #define    SHOW_INT_VALUE_USED(Entered, Used)\
  20.     printf("\tTest Value Used: %d%s\n", Used,\
  21.     (Entered == Used) ? "" : " (Clamped)")
  22.  
  23. static    double    PokerProbs[MAX_CARDS];
  24. /* ==================================================================== */
  25. /* SetPokerControls - Puts Poker-Test controls in PokerData structure    */
  26. /* ==================================================================== */
  27. void
  28. SetPokerControls(POKER_DATA_STRU * PokerData)
  29. {
  30.     int     j, NewlineCh;
  31.     int     UserIntEntry;
  32.     long    UserLongEntry;
  33.  
  34.     NewlineCh = _isatty(_fileno(stdin)) ? '\r' : '\n';
  35.  
  36.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  37.     /* -------------------------------------- */
  38.     /* Request Number of Cards Per Hand [4-8] */
  39.     /* -------------------------------------- */
  40.     GetInt(NEED_CARDS_PER_HAND(MIN_CARDS, MAX_CARDS),
  41.     &UserIntEntry);
  42.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  43.     printf("\n\tNumber Entered:  %d", UserIntEntry);
  44.     printf(" (Cards Per Hand)\n");
  45.     /* ------------------ */
  46.     /* Clamp CardsPerHand */
  47.     /* ------------------ */
  48.     PokerData->CardsPerHand =
  49.     __min(MAX_CARDS, __max(MIN_CARDS, UserIntEntry));
  50.  
  51.     SHOW_INT_VALUE_USED(UserIntEntry, PokerData->CardsPerHand);
  52.  
  53.     /* --------------------------------------------- */
  54.     /* Request Number of Unique Cards in Random Deck */
  55.     /* --------------------------------------------- */
  56.     GetInt(NEED_NUMBER_UNIQUE_CARDS(MIN_DECK_SIZE, MAX_DECK_SIZE),
  57.     &UserIntEntry);
  58.  
  59.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  60.     printf("\n\tNumber Entered:  %d", UserIntEntry);
  61.     printf(" (Number Unique Cards in Random Deck\n");
  62.     /* -------------- */
  63.     /* Clamp DataSize */
  64.     /* -------------- */
  65.     PokerData->DataSize =
  66.     __min(MAX_DECK_SIZE, __max(MIN_DECK_SIZE, UserIntEntry));
  67.  
  68.     SHOW_INT_VALUE_USED(UserIntEntry, PokerData->DataSize);
  69.  
  70.     /* -------------------------------- */
  71.     /* Request Minimum Cell Expectation    */
  72.     /* -------------------------------- */
  73.     GetInt(NEED_MIN_CELL_EXPECT(MIN_CELL_XPCT, MAX_CELL_XPCT),
  74.     &UserIntEntry);
  75.  
  76.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  77.     printf("\n\tNumber Entered:  %d", UserIntEntry);
  78.     printf(" (Cell Expectation)\n");
  79.  
  80.     /* ---------------- */
  81.     /* Clamp CellExpect */
  82.     /* ---------------- */
  83.     PokerData->UserCellExpect =
  84.     __min(MAX_CELL_XPCT, __max(MIN_CELL_XPCT, UserIntEntry));
  85.  
  86.     SHOW_INT_VALUE_USED(UserIntEntry, PokerData->UserCellExpect);
  87.  
  88.     /* -------------------------------------------------- */
  89.     /* Calculate Cell Expectations Based on Probabilities */
  90.     /* -------------------------------------------------- */
  91.     CalcPokerProbs(PokerData->CardsPerHand, PokerData->DataSize,
  92.     PokerProbs);
  93.  
  94.     PokerData->IdealNumHands = (ULONG)
  95.     (((double)MIN_CELL_XPCT + PokerProbs[0])/PokerProbs[0]);
  96.  
  97.     PokerData->UserNumHands = (ULONG)
  98.     ((PokerData->UserCellExpect + PokerProbs[0])/PokerProbs[0]);
  99.     {
  100.     char    Prompt[64];
  101.     int    SpareCards;
  102.  
  103.     SpareCards = PokerData->CardsPerHand - MIN_CARDS;
  104. printf("# Spare Cards = %d, PokerProbs = %.15g\n",
  105. SpareCards, PokerProbs[SpareCards]);
  106.     sprintf(Prompt,
  107.         "How Many Hands Per Run Should Be Dealt? [%.f ...]: ",
  108.         floor(0.5 + PokerData->UserCellExpect/PokerProbs[SpareCards]));
  109.  
  110.     GetLong(Prompt, &UserLongEntry);
  111.     }
  112.  
  113.     fflush(NULL);fprintf(stderr, "%c", NewlineCh);
  114.     printf("\n\tNumber Entered:  %ld", UserLongEntry);
  115.     printf(" (Number of Hands to Be Dealt)\n");
  116.     printf("\tTest Value Used: %ld\n", UserLongEntry);
  117.  
  118.     PokerData->NumHands = UserLongEntry;
  119.  
  120.     /* ------------------------------- */
  121.     /* Calculate Category Expectations */
  122.     /* ------------------------------- */
  123.     for (j = 0; j < PokerData->CardsPerHand; ++j)
  124.     {
  125.     PokerData->CellExpect[j] = PokerProbs[j] * PokerData->NumHands;
  126.     P(printf("PokerProbs[%2d] = %.11e, ", j, PokerProbs[j]));
  127.     P(printf("CellExpect[%2d] = %.11e\n", j, PokerData->CellExpect[j]));
  128.     }
  129.  
  130.     PokerData->NotEnough = 0;
  131.     /* -------------------------------------------------- */
  132.     /* Lump lower-valued categories into next higher ones */
  133.     /* -------------------------------------------------- */
  134.     for (j = 0; j < PokerData->CardsPerHand-1; ++j)
  135.     {
  136.     /* if (PokerData->CellExpect[j] < 1.0) */
  137.     if (PokerData->CellExpect[j] < (double)PokerData->UserCellExpect)
  138.     {
  139.         ++PokerData->NotEnough;
  140.         PokerData->CellExpect[j+1] += PokerData->CellExpect[j];
  141.         PokerData->CellExpect[j]    = 0;
  142.     }
  143.     }
  144.     P(printf("NotEnough = %d\n", PokerData->NotEnough));
  145.     PokerData->CallStatusOK = (PokerData->NotEnough == 0);
  146. }
  147.